<?php
include '../conn.php';
$data = json_decode($HTTP_RAW_POST_DATA);
$avatar = "";
$sql = "select * from users_info
        where username = '$data->username' or nickname = '$data->nickname'
        limit 1";
$req = $pdo->getOne($sql);
if($req){
    $arr = [
        "code" => 400,
        "desc" => "昵称或账号已存在"
    ];
}else{
    foreach($data->avatar as $img):
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $result)){
            $type = $result[2];
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $new_file = "../images/avatar/$image_name";
            $img_url = substr($new_file,2);
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img)))){
                $avatar = "$img_url";
            };
        };
    endforeach;
    $sql = "insert into users_info (username,password,nickname,avatar) values ('$data->username','$data->password','$data->nickname','$avatar')";
    $arr = [
        "code" => 200,
        "desc" => "注册成功"
    ];
}
echo json_encode($arr);